package com.tianqu.tms.core.util.car;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.WeChatMqApi;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn000602Bo;
import com.tianqu.tms.dao.TmsCarModelDaoImp;
import com.tianqu.tms.dao.TmsCarmodelVinDaoImp;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TmsSmsInfoDaoImp;
import com.tianqu.tms.dto.TmsCarModelDto;
import com.tianqu.tms.dto.TmsCarmodelVinDto;
import com.tianqu.tms.dto.TmsSmsInfoDto;

public class TmsCarModelInfoApi extends TmsEntity{
	private CarInformationApi API = new CarInformationApi();
	private Logger LOGGER = Logger.getLogger(TmsCarModelInfoApi.class);
	
	private static final TmsCarModelInfoApi instance = new TmsCarModelInfoApi();
	
	private TmsCarModelInfoApi (){}
	
	public static TmsCarModelInfoApi getInstance(){
		return instance;
	}
	//
	public TmsCarModelDto CarModelGet(String id) throws  TmsDaoException{
		TmsCarModelDaoImp mDao = (TmsCarModelDaoImp) TmsDao.getInstance(TmsDao.TMS_CAR_MODEL);
		return mDao.getCarModel(id);
	}
	
	public void carModelUpdate(TmsCarModelDto bean) throws TmsDaoException{
		TmsCarModelDaoImp mDao = (TmsCarModelDaoImp) TmsDao.getInstance(TmsDao.TMS_CAR_MODEL);
		mDao.update4Api(bean);
	}

    //
	public TmsCarmodelVinDto carmodelVinGet(String modelId,String vin) throws TmsDaoException{
		TmsCarmodelVinDaoImp mDao = (TmsCarmodelVinDaoImp) TmsDao.getInstance(TmsDao.TMS_CAR_MODEL_VIN);
		return mDao.get(modelId, vin);
	}
	
	public void carmodelVinInsert(TmsCarmodelVinDto bean) throws TmsDaoException{
		TmsCarmodelVinDaoImp mDao = (TmsCarmodelVinDaoImp) TmsDao.getInstance(TmsDao.TMS_CAR_MODEL_VIN);
		mDao.insert(bean);
	}

	public void carmodelVinUpdate(TmsCarmodelVinDto bean) throws TmsDaoException{
		TmsCarmodelVinDaoImp mDao = (TmsCarmodelVinDaoImp) TmsDao.getInstance(TmsDao.TMS_CAR_MODEL_VIN);
		mDao.update(bean);
	}
	
	public void insertinsertSmsInfo(TmsSmsInfoDto bean) throws TmsDaoException{
		TmsSmsInfoDaoImp mDao = (TmsSmsInfoDaoImp) TmsDao.getInstance(TmsDao.TMS_SMS_INFO);
		mDao.insertinsertSmsInfo(bean);
	}
	
	//1）通过传入的vin码到车款和VIN码对应表查询对应车型id
	public String getCarModelInfoByVin(String vin) throws TmsDaoException{
		//
		String modelId = "";
		try {
			TmsCarmodelVinDto carmodelVin = carmodelVinGet(null,vin);
			if(carmodelVin == null || StringUtil.isNullOrBlank(carmodelVin.getModelId())){
				IdentifyModelByVinBean info = API.getIdentifyModelByVin(vin);
                if(!StringUtil.isNullOrBlank(info.getModelId())){
                	modelId = info.getBrandId();
                	TmsCarModelDto tmsCarModelDto  = CarModelGet(info.getModelId());
                	if(tmsCarModelDto!=null){
                		//更新 TmsCarModel
                		tmsCarModelDto.setPrice(info.getModelPrice());
                		tmsCarModelDto.setYear(info.getModelYear());
                		tmsCarModelDto.setEmissionStangard(info.getModelEmissionStangard());
                		tmsCarModelDto.setMaxRegYear(info.getMaxRegYear());
                		tmsCarModelDto.setMinRegYear(info.getMinRegYear());
                		carModelUpdate(tmsCarModelDto);
                		
                		//查询 tms_carModel_Vin
                		TmsCarmodelVinDto tmsCarmodelVinDto = carmodelVinGet(null,vin);
                		if(tmsCarmodelVinDto != null){//存在 更新
                			tmsCarmodelVinDto.setModelId(tmsCarModelDto.getId());
                			carmodelVinUpdate(tmsCarmodelVinDto);
                		}else{//不存在插入
                			tmsCarmodelVinDto =  new TmsCarmodelVinDto();
                			tmsCarmodelVinDto.setModelId(tmsCarModelDto.getId());
                			tmsCarmodelVinDto.setVin(vin);
                			carmodelVinInsert(tmsCarmodelVinDto);
                		}
                	}
                }else{//api没有找到数据
                	String userId = TmsConstants.DEBUG_USER_TEL;
                	String pushContent = "车型VIN为"+vin+",对应得到的车型信息在车辆型号表不存在，请根据微信通知记录表保存信息进行补充";
                	
                	Date now = new Date();
            		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            		String insertTime = dateFormat.format(now);
                	 
            		TmsSmsInfoDto sms  = new TmsSmsInfoDto();
            		sms.setSerial("00000");
            		sms.setSmsContent(pushContent);
            		sms.setInsertUserId(3);
            		sms.setOwnerPhone(userId);
    				 //将当前时间放到短信记录中
            		sms.setInsertTime(insertTime);
            		insertinsertSmsInfo(sms);
    				
					//接口编号：602【TMS_web】=》微信终端
					TransferDataSyn000602Bo transferDataSyn000602Bo = new TransferDataSyn000602Bo();
					transferDataSyn000602Bo.setUserId(userId);
					transferDataSyn000602Bo.setPushContent(pushContent);			
					WeChatMqApi.getInstance().call(transferDataSyn000602Bo,false);
                }
			}else{
				modelId = carmodelVin.getModelId();
			}
		} catch (TmsDaoException e) {
			LOGGER.error(e);
		}
		return modelId;
	}

	public IdentifyModelByVinBean getIdentifyModelByVin(String vin){
		//
		IdentifyModelByVinBean info = new IdentifyModelByVinBean();
		try {
			TmsCarmodelVinDto carmodelVin = carmodelVinGet(null,vin);
			if(carmodelVin == null || StringUtil.isNullOrBlank(carmodelVin.getModelId())){
				info = API.getIdentifyModelByVin(vin);
				info.setCallApi(true);
				if(!StringUtil.isNullOrBlank(info.getModelId())){
					TmsCarModelDto tmsCarModelDto  = CarModelGet(info.getModelId());
					if(tmsCarModelDto!=null){
						//更新 TmsCarModel
						tmsCarModelDto.setPrice(info.getModelPrice());
						tmsCarModelDto.setYear(info.getModelYear());
						tmsCarModelDto.setEmissionStangard(info.getModelEmissionStangard());
						tmsCarModelDto.setMaxRegYear(info.getMaxRegYear());
						tmsCarModelDto.setMinRegYear(info.getMinRegYear());
						carModelUpdate(tmsCarModelDto);

						//查询 tms_carModel_Vin
						TmsCarmodelVinDto tmsCarmodelVinDto = carmodelVinGet(null,vin);
						if(tmsCarmodelVinDto != null){//存在 更新
							tmsCarmodelVinDto.setModelId(tmsCarModelDto.getId());
							carmodelVinUpdate(tmsCarmodelVinDto);
						}else{//不存在插入
							tmsCarmodelVinDto =  new TmsCarmodelVinDto();
							tmsCarmodelVinDto.setModelId(tmsCarModelDto.getId());
							tmsCarmodelVinDto.setVin(vin);
							carmodelVinInsert(tmsCarmodelVinDto);
						}
					}
				}
			}else {
				info.setModelId(carmodelVin.getModelId());
			}
		} catch (TmsDaoException e) {
			LOGGER.error(e);
		}
		return info;
	}
	
	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub
		
	}

	 
 
}
